using System.Collections.Generic;
using System.Drawing;
using TouchSmart.Animations;
using TouchSmart.Drawing;
using TouchSmart.Gestures;

namespace TouchSmart.Widgets
{
    public class ListBox : Widget
    {
        private readonly IImage _background;

        private List<ListItem> Items { get; set; }
        private Point _lastLocation = Point.Empty;
        private readonly Animation _animation = new DecelerationAnimation(0.0f, 12.0f);

        public ListBox(Point location, Size size) : base(location, size)
        {
            Items = new List<ListItem>();

            this._background = Locator.GetService<IImageFactory>().CreateImage("Background.bmp");
        }

        public override void Draw(Graphics g)
        {
            g.DrawImageAlphaChannel(this._background, Location.X, Location.Y);

            foreach (var item in Items)
            {
                item.Draw(g);
            }
        }

        public override void OnMovement(MovementEventArgs e)
        {
            foreach (var item in Items)
            {
                item.OnMovement(e);
            }
        }

        public override void OnMouseDown(ClickEventArgs e)
        {
            base.OnMouseDown(e);
            this._animation.Stop();
        }

        public override void OnSlide(SlideEventArgs e)
        {
            this._animation.Start();
        }

        public void AddItem(ListItem item)
        {
            if(this._lastLocation == Point.Empty)
            {
                this._lastLocation = new Point(4, 4);
            }
            else
            {
                this._lastLocation = new Point(this._lastLocation.X, this._lastLocation.Y + item.Size.Height + 4);
            }

            item.Location = this._lastLocation;
            Items.Add(item);
            this._animation.AnimateOn(item);
        }
    }
}